import axios from "axios";
import { Message } from "element-ui";
import cookie from 'cookie_js'
import { getToken, getUsername } from './app'

// 创建axios
const BASEURL = process.env.NODE_ENV === "production" ? "" : "/devApi";
const service = axios.create({
  baseURL: BASEURL, // http:192.168.0.106:8080/devApi == http://www.web-jshtml.cn/productapi
  timeout: 15000
});
// 请求拦截器
service.interceptors.request.use(
  function(config) {
    config.headers['Tokey'] = getToken()
    config.headers['Username'] = getUsername()
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  function(response) {
    let data = response.data;
    if (data.resCode !== 0) {
      Message.error(data.message);
      return Promise.reject(data);
    } else {
      Message.success(data.message)
      return response;
    }
  },
  function(error) {
    return Promise.reject(error);
  }
);

export default service;
/* 
    export default 文件引入不需要{} 智能用一次
    export可以使用多次 需要{} 引入
    
*/
